Ext.require(['AM.service.WorkSpace', 'AM.service.ResizePage']);

Ext.define('AM.service.Thumbnails', {

    add:function (index) {
        var ww = ppt.view.workspace.getWidth();
        var wh = ppt.view.workspace.getHeight();
        var tw = ppt.view.thumbnails.getWidth() * 0.9;

        var times = tw / ww;
        var th = wh * times;

        var dom = AM.service.ResizePage.getThumbnailsDom(index, times);

        var thPage = Ext.widget('container', {
            id:'th-' + Math.random(),
            width:tw * 1.1,
            height:th * 1.2
        });

        ppt.view.thumbnails.add(thPage);
        thPage.el.dom.appendChild(dom)

        var el = Ext.get(dom)
        el.addListener('click', function () {
            Ext.each(ppt.view.thPages, function(p){
                p.el.removeCls('currentTh');
            }) ;
            thPage.el.addCls('currentTh');
            AM.service.WorkSpace.go(index);
        });

        if (ppt.view.thPages == null) {
            ppt.view.thPages = [thPage];
        }
        else {
            ppt.view.thPages.push(thPage);
        }
    },



    singleton: true,

    delete:function (index) {
        var page = ppt.view.thPages[index];
        Ext.destroy(page);
        delete page;
    },



    activeThPage: function(index){
        Ext.each(ppt.view.thPages, function(p, index2){
            p.el.removeCls('currentTh');
            if (index == index2) {
                p.el.addCls('currentTh');
            }
        })
    },

    refresh:function (index) {
        var index = index ? index : ppt.index

        var ww = ppt.view.workspace.getWidth();
        var wh = ppt.view.workspace.getHeight();
        var tw = ppt.view.thumbnails.getWidth();

        var times = tw / ww;
        var th = wh * times;

        var dom = AM.service.ResizePage.getThumbnailsDom(index, times);
      //  var el = Ext.get('th-' + index)

        var page = ppt.view.thPages[index];
        var el = page.el;
        el.dom.innerHTML = "";
        el.appendChild(dom);
        el.addListener('click', function () {
            Ext.each(ppt.view.thPages, function(p){
                p.el.removeCls('currentTh');
            })
            el.addCls('currentTh');
            AM.service.WorkSpace.go(index);
        })
    }
});